package com.north.light.androidutils.permission;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.north.light.androidutils.gson.GsonUtils;
import com.north.light.androidutils.log.LogUtil;
import com.north.light.libpermission.PermissionUtils;
import com.north.light.libpermission.api.PermissionCallback;
import com.north.light.libpermission.constant.Permission;

import java.util.List;

/**
 * FileName: PermissionTestView
 * Author: lzt
 * Date: 2022/8/25 08:57
 */
public class PermissionTestView extends LinearLayout {
    public PermissionTestView(Context context) {
        super(context);
        init();
    }

    public PermissionTestView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PermissionTestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        PermissionUtils.with((Activity) getContext())
                .key(1111)
                .intercept(new PermissionIntercept1())
                .request(new String[]{Permission.READ_PHONE_STATE})
                .setListener(new PermissionCallback() {
                    @Override
                    public void result(int type, int status, boolean grantAll) {
                        LogUtil.d("result： type" + type + " status: " + status + " grantAll: " + grantAll);
                    }

                    @Override
                    public void result(int type, int status, List<String> permissions, boolean grantAll) {
                        LogUtil.d("result2： type" + type + " status: " + status + " permissions: " + GsonUtils.getJsonStr(permissions) + " grantAll: " + grantAll);
                    }

                    @Override
                    public void error(int code) {
                        LogUtil.d("result2： error" + code);
                    }
                }).start();
    }
}
